package com.koudai.finance.service.impl;

import com.google.common.collect.Lists;
import com.koudai.finance.dao.TradeDao;
import com.koudai.finance.dao.UserDao;
import com.koudai.finance.entity.Trade;
import com.koudai.finance.service.CommonService;
import com.koudai.finance.service.UserService;
import com.koudai.finance.utils.AmountUtils;
import com.koudai.finance.vo.TradeVO;
import com.koudai.finance.vo.UserAccountVO;
import com.koudai.finance.web.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 登录服务
 * @author: ls
 * @Date: 2020/7/10 14:00
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private CommonService commonServiceImpl;

    @Resource
    private UserDao userDao;

    @Resource
    private TradeDao tradeDao;

    /**
     * @Description： 获取用户账户信息
     * @Author: ls
     * @Date: 2020/11/10 4:35 下午
     */
    @Override
    public UserAccountVO getAccount() {
        Integer userId = UserContext.getUserId();
        UserAccountVO vo = UserAccountVO.builder().build();
        return vo;
    }

    /**
     * @Description: 修改密码
     * @Author ls
     * @Date 2023/03/05 11:15
     */
    @Override
    public void updatePassword(String oldPwd, String password) {
    }

    /**
     * @Description: 获取交易列表
     * @Author ls
     * @Date 2023/03/05 11:15
     */
    @Override
    public Map<String, Object> getTradeList() {
        Integer userId = UserContext.getUserId();
        List<Trade> tradeList = tradeDao.getTradeList(userId);
        Map<String, Object> resultMap = commonServiceImpl.buildQryResultMap(tradeList);
        if (CollectionUtils.isEmpty(tradeList)) {
            return resultMap;
        }
        List<TradeVO> voList = Lists.newArrayListWithExpectedSize(tradeList.size());
        for (Trade trade : tradeList) {
            TradeVO tradeVO = TradeVO.builder().build();
            BeanUtils.copyProperties(trade, tradeVO);
            tradeVO.setTotalFee(AmountUtils.changeF2Y(trade.getTotalFee()) + "元");
            voList.add(tradeVO);
        }
        resultMap.put("list", voList);
        return resultMap;
    }

}